package soft.quicknote.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import soft.quicknote.DBHelper;
import soft.quicknote.R;
import soft.quicknote.adapter.CategoryAdapter;
import soft.quicknote.beans.Category;

public class CategoryEditor extends AppCompatActivity {
    private static final String TAG = CategoryEditor.class.getSimpleName();

    DBHelper helper;
    CategoryAdapter adapter;
    RecyclerView mRecycleView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_editor);

        helper = new DBHelper(this);
        adapter = new CategoryAdapter(this, helper.queryAllCategory());

        mRecycleView = (RecyclerView) findViewById(R.id.cat_list);
        mRecycleView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        mRecycleView.setItemAnimator(new DefaultItemAnimator());
        mRecycleView.setAdapter(adapter);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_category_editor, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Category cat;
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.menu_item_save_cat:
                helper.saveManyCategories(adapter.mList);
                Toast.makeText(this, "保存所有分类成功!", Toast.LENGTH_LONG).show();
                return true;
            case R.id.menu_item_add_cat:
                cat = helper.insertCategory(new Category(-1, "新分类"));
                adapter.mList.add(0, cat);
                adapter.notifyItemInserted(0);
                return true;
            case R.id.menu_item_remove_cat:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("确定删除所选项？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        removeCats();
                        dialog.dismiss();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    void removeCats(){
        Category cat = null;
        int cnt = 0;
        for (int i = 0; i < adapter.mList.size(); ) {
            cat = adapter.mList.get(i);
            if (cat.isChecked()) {
                cnt++;
                if (helper.deleteSingleCategory(cat.getId())) {
                    adapter.mList.remove(cat);
                    mRecycleView.removeViewAt(i);
                    adapter.notifyItemRemoved(i);
                }
            } else {
                i++;
            }
        }
        if (cnt == 0) {
            Toast.makeText(this, "没有选中的项!", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "已删除 " + cnt + " 项", Toast.LENGTH_LONG).show();
        }
    }
}
